/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.srcptit.trainning.thompv.BaitapMuc1_2;
import  java.util.Scanner;
/**
 *
 * @author SONY
 */
public class Bai5Muc2 {
    static int n,ok=1;
    static int []a=new int[1000];
    public static void Init()
    {
        for(int i=1;i<=n;i++)
            a[i]=i;
    }
    public static void in()
    {
        for(int i=1;i<=n;i++)
            System.out.print(a[i]+" ");
        System.out.println("");
    }
    public static void sinh()
    {
        int j=n-1;
        while(j>0&&a[j]>a[j+1]) j--;
        if(j>0)
        {
             int k=n;
             while(a[j]>a[k]) k--;
             int tmp=a[j]; a[j]=a[k]; a[k]=tmp;
             int r=j+1,s=n;
             while(r<=s)
             {
                 int tam=a[r]; a[r]=a[s]; a[s]=tam;
                 r++; s--;
             }
        }
        else ok=0;
    }
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int stop=1;
        String s;
        System.out.print("Nhap do dai N = ");
        while(stop==1)
        {
            try
            {
                s=sc.nextLine();
                if(s.length()<1) throw new Exception("");
                n=Integer.parseInt(s);
                if(n<=0) throw new Exception("");
                stop=0;
                Init();
                System.out.println("Ket qua:");
                while(ok==1)
                {
                    in();
                    sinh();
                }
            }
            catch (Exception e)
            {
                System.out.println("Nhap lai");
            }
        }
    }
}
